 
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room pen,book,bag,foot1,face1,hair1,he,cat,cat2,dog,dog2,pig,pig2,pig3,his,his2,turkey,boy,boy2,robot;
        
        // create the rooms
        foot1 = new Room("一间奇怪的房间");
        face1 = new Room("一间安全的房间？",new Thing("矿泉水",30));
        hair1 = new Room("一间学生教室", new Thing("面包",40));
        pen = new Room("开始大厅", new Thing("糖果",20));
        pig3 = new Room("这个房间有一点香味", new Thing("草莓",20));
        he = new Room("一间奇怪的房间", new Monster("飞镖机关",40));
        cat = new Room("一间电脑房", new Monster("蜘蛛",30));
        dog = new Room("一间音乐房", new Monster("大锤机关",30));
        dog2 = new Room("一间画室，要不要查看一下呢？");
        pig = new Room("一间简陋的房间。");
        pig2 = new Room("一间普通的房间。");
        his = new Room("一个蓝色门的房间", new Monster("尖刀机关",30));
        his2 = new Room("一间好像有人来过的房间。");
        turkey = new Room("这里难道是？！", new Monster("滚石机关",40));
        boy= new Room("一间奇妙的房间。");
        boy2= new Room("一间好像来过的房间。");
        robot = new Room("这里有一个红色的门", new Monster("出口",0));
        
        pen.setExit("north",foot1);
        pen.setExit("east",face1);
        pen.setExit("south",hair1);
        foot1.setExit("east",he);
        foot1.setExit("south",pen);
        face1.setExit("east",cat);
        face1.setExit("north",he);
        face1.setExit("west",pen);
        hair1.setExit("north",pen);
        hair1.setExit("south",dog);
        hair1.setExit("west",dog2);
        he.setExit("east",pig);
        he.setExit("south",face1);
        he.setExit("west",foot1);
        cat.setExit("north",pig);
        cat.setExit("south",pig2);
        cat.setExit("west",he);
        dog.setExit("north",hair1);
        dog2.setExit("north",his);
        dog2.setExit("west",his2);
        dog2.setExit("east",hair1);
        pig.setExit("south",cat);
        pig.setExit("west",he);
        pig2.setExit("west",pig3);
        pig2.setExit("north",cat);
        pig3.setExit("east",pig2);
        his.setExit("west",turkey);
        his.setExit("south",dog2);
        his2.setExit("north",turkey);
        his2.setExit("east",dog2);
        turkey.setExit("north",boy);
        turkey.setExit("east",his);
        turkey.setExit("south",his2);
        boy.setExit("north",robot);
        boy.setExit("south",turkey);
        boy.setExit("east",boy2);
        boy2.setExit("west",boy);
        robot.setExit("south",boy);
       
        currentRoom = pen;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("游戏结束");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        
        System.out.println("一阵风吹来你被冷醒了。");
        System.out.println("你看了一下附件发现不是自己的房间，此时一个电视屏幕出现。");
        System.out.println("里面的人说着：欢迎来到密室逃脱游戏，请尽情的挣扎逃离这里吧。");
        System.out.println("你现在身处" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        Thing thing = currentRoom.getThing();

        if(command.isUnknown()) {
            System.out.println("没有该选项");
            return false;
        }
 
        Word commandWord = command.getCommandWord();
        
        switch(commandWord) {
 
            case GO:
                goRoom(command);
                break;
            case LOOK:
                say(currentRoom);
                break;
            case PICK:
                player.addToBag(thing);
                currentRoom.remove();
                break;
            case CHECK:
                player.checkAndPrint();
                break;
            case EAT:
                player.eatThing(command);
                break;

        }
        return wantToQuit;
    }
    
    private void say(Room currentRoom) {
        System.out.print("这里");
        if (currentRoom.getThing() == null) {
            System.out.println("没有任何物品");
        }
        else System.out.println("有"+currentRoom.getThing().getName());
        if (currentRoom.getMonster() == null) {
            System.out.println("没有发现陷阱");
        }
        else if ("出口" == currentRoom.getMonster().getMonsterName()) {
            System.out.println("你到达出口，恭喜你，挑战成功！");
            System.exit(0);
        }
        else {
          player.hurt(currentRoom.getMonster().getDamage());
          System.out.println("你不小心启动了机关,这是" + currentRoom.getMonster().getMonsterName() + "!");
          System.out.println("你受到" + currentRoom.getMonster().getDamage() + "点伤害!");
          System.out.println("你摆脱了陷阱，逃跑了。");
          currentRoom.removeMonster();
        }
    }
    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */


    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("到哪里?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            System.out.println("这里无法进入");
        }
        else {
            player.step();
            System.out.println("您消耗了10点体力, "+"您目前的体力是: "+player.getStrength());
            currentRoom = nextRoom;
            System.out.println("你现在身处" + currentRoom.getDescription());
            currentRoom.printExits();
        }
    }

    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
 
}
